%% VAR pre-Civil War
clearvars -except Globaloption option

data = readtable('../../data/main_VAR/US_full.xlsx');
date = data.year;

breakyear = 1860;

startdatenum = 1793;
enddatenum = breakyear;

loaddata_4CIjoint_annual_no_year;

save(['MAT/res_annual_VAR_nodebt_US_pre', num2str(breakyear), '.mat'], '-regexp', '^(?!(option|Globaloption)$).');

%% CS decomposition pre-Civil War
% upper bound calculation

option.bootstrap = 1;
Campbell_Shiller_US_Benchmark_append;
upper = exp(pxbar) * (mean(taxrevgdp - spendgdp));

gdpreturn = output_rp + y0nom_1 + yspr0 - pi0;
breakyear = 1860;

if option.bootstrap == 1
    save (['MAT/US_pre', num2str(breakyear), '_bootstrap.mat'], 'k0x', 'k1x', 's', 'upper', 'pdX', 'pxbar', 'gdpreturn', 's', 'std_coeff', 'gdebt', 'output_rp', 'pdT', 'pdG', 'CFT', 'CFG', 'DR', 'taxrevgdp', 'spendgdp')
elseif option.bootstrap == 0
    save (['MAT/US_pre', num2str(breakyear), '.mat']) %k0x k1x" "s" "upper" "pdX" "pxbar" "gdpreturn"
end

%% VAR post-Civil War
clear all;
clc;
data = readtable('../../data/main_VAR/US_full.xlsx');
date = data.year;

breakyear = 1860;

startdatenum = breakyear + 1;
enddatenum = 1946;

loaddata_4CIjoint_annual_no_year;
save(['MAT/res_annual_VAR_nodebt_US_post', num2str(breakyear), '.mat'], '-regexp', '^(?!(option|Globaloption)$).');

%% CS decomposition post-Civil War
% upper bound calculation

option.bootstrap = 1;

Campbell_Shiller_US_Benchmark_append;
upper = exp(pxbar) * (mean(taxrevgdp - spendgdp));

gdpreturn = output_rp + y0nom_1 + yspr0 - pi0;

breakyear = 1860;

if option.bootstrap == 1
    save (['MAT/US_post', num2str(breakyear), '_bootstrap.mat'], 'k0x', 'k1x', 's', 'upper', 'pdX', 'pxbar', 'gdpreturn', 's', 'std_coeff', 'gdebt', 'output_rp', 'pdT', 'pdG', 'CFT', 'CFG', 'DR', 'taxrevgdp', 'spendgdp')
elseif option.bootstrap == 0
    save (['MAT/US_post', num2str(breakyear), '.mat'])
end

%% Plot CS results
breakyear = 1860;

load (['MAT/US_pre', num2str(breakyear), '_bootstrap.mat'])

f = figure;
ylimit = [1.5 1.5];
plot_CI(1793:1860, s, std_coeff, gdebt, upper, ylimit)
ylim([-1.5 1.5])

f.PaperSize = [6 3.75];

print(['../../results/figures_main/US/pre1946/cs_US_pre', num2str(breakyear), '_', num2str(output_rp * 1000), 'TwoVAR'], '-dpng');

load (['MAT/US_post', num2str(breakyear), '_bootstrap.mat'])
breakyear = 1860;

startdatenum = breakyear + 1;
enddatenum = 1946;

f = figure;

ylimit = [1.5 1.5];
plot_CI(startdatenum:enddatenum, s, std_coeff, gdebt, upper, ylimit)

f.PaperSize = [6 3.75];

print(['../../results/figures_main/US/pre1946/cs_US_post', num2str(breakyear), '_', num2str(output_rp * 1000), 'TwoVAR'], '-dpng');
